
import axios from "axios";
import { getData, getSettings, saveSettings } from "./db";
import utils from "./utils";
import dayjs from "dayjs";

interface RegouResponse {
  status: boolean;
  data: {
    id: number;
    text1: string;
    text2: string;
    createdAt: string;
  }[];
}

export async function regouJob() {
  console.log('regou job ' + dayjs().format('MM-DD HH:mm:ss'))
  const { data: response } = await axios.get<RegouResponse>('https://regou.minapp.xin/api/send');
  const lastId = await getSettings("", 'regou_last_id') || 0;
  const lastSend = response.data[0];

  if (lastSend.id !== lastId) {
    await saveSettings("", 'regou_last_id', lastSend.id);
    
    const data = await getData();
    const chats = Object.entries(data.settings)
      .filter(([key, value]) => key.startsWith('regou_status_') && value)
      .map(([key]) => key.replace('regou_status_', ''));

    for (const chat of chats) {
      await utils.sendMessage(chat, `热狗推送：\n${lastSend.text1}\n${lastSend.text2}`);
    }
  }
}